----------------------------------Spell Variables--------------------------------------
SPELL.m_iSpellID = 1200 -- Whatever value you want as long as it doesn't override any other spells' IDs.
SPELL.m_iRange = 1024
SPELL.m_sName = "Fireball"
SPELL.m_sToolTip =	[[Launch a ball of molten
					  rock at the target, dealing
					  35 fire damage.]]
SPELL.m_sIcon = "devin/blackbetty/spells/mage/fireball.png"
SPELL.m_iCoolDown = 0.25
SPELL.m_iManaCost = 50
SPELL.m_bRequiresTarget = true
SPELL.CastTime = 0.25
SPELL.CastSound = nil
SPELL.m_iSpellType = SPELLTYPE_MAGICDAMAGE


---------------------------------------------------------------------------------------

function SPELL:Cast( pl, target )
	if(pl.DoingCast)then
		return true
	end
	local castingloop = "devin/blackbetty/weapons/travelsounds/FireMissileLoop.wav"
	local spellhit = "devin/blackbetty/weapons/hit/Fire1.wav"
	
	if(target && target:IsValid() && target != pl && pl:GetPos():Distance(target:GetPos()) < self.m_iRange) then
		self.CastSound = CreateSound( pl, "devin/blackbetty/weapons/castingloops/Fire.wav" )
		self.CastSound:Play()
		pl:SetNWString("HoldType", "camera")
		pl.DoingCast = true
		pl.CastWhileMoving = true
		net.Start( "SendPlayerCast" )
		local SendTable = {self.CastTime, self.m_sName, Color(155, 15, 15, 155)}
		net.WriteTable(SendTable)
		net.Send(pl)
		
		if (pl:GetPos():Distance(target:GetPos()) < self.m_iRange) then
		
			print("Casted "..self.m_sName.."!")
			ParticleEffectAttach( "fire_hands", PATTACH_POINT_FOLLOW, pl, pl:LookupAttachment("anim_attachment_RH"))
			timer.Simple(self.CastTime-0.35, function() 
				pl:SetNWBool("Casting", false)
				pl:SetNWBool("Casted", false)
				if(pl and pl:IsValid() and pl:IsPlayer())then
					pl:DoAnimationEvent(ACT_GMOD_GESTURE_MELEE_SHOVE_2HAND) 
				elseif(pl and pl:IsValid() and !pl:IsPlayer())then
					pl:RestartGesture(ACT_RANGE_ATTACK_THROW)
				else
					self.CastSound:Stop()
				end
			end)
			timer.Create("casting_"..pl:GetName().."", self.CastTime, 1, function()
				if !pl or !target then
					pl.DoingCast = false
					self.CastSound:Stop()
					pl:StopParticles()
					pl:SetNWBool("Casting", false)
					pl:SetNWBool("Casted", false)
					return true
				end
				if pl.DoingCast then
					local myhitdelay = 90
					LaunchProjectile(pl, target, 0, "pummel_hit", "", "blood_hands", "ambient/atmosphere/city_tone.wav", myhitdelay, 200, "models/mossman.mdl")
					timer.Simple(myhitdelay, function()
						target:EmitSound("devin/blackbetty/weapons/hit/Ice1.wav", 100, 100+math.random(-10,10))
						local dmginfo = DamageInfo()
							dmginfo:SetDamage( 1337 )
							dmginfo:SetDamageType( DMG_GENERIC ) 
							dmginfo:SetAttacker( pl )
						target:TakeDamageInfo( dmginfo )
						target:EmitSound("devin/blackbetty/weapons/bow/ArrowHit0"..math.random(1,3)..".wav", 100, 100+math.random(-10,10))
						ParticleEffect( "pummel_hit", target:GetPos() + Vector(0, 0, 45), Angle(0,0,0), target )

					end)
				end
				pl.DoingCast = false
				self.CastSound:Stop()
				pl:StopParticles()
				pl:SetNWBool("Casting", false)
				pl:SetNWBool("Casted", false)
			end)
		end
	end
end




